﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using Yuchen.NPA.Core;

namespace Yuchen.NPA.EntityFrameworkCore
{
    public class NPADbContext : DbContext
    {
        public NPADbContext(DbContextOptions options):base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var types = EntityMetaData.EntityTypes.Where(InThisDbContext);
            var entityTypes = modelBuilder.Model.GetEntityTypes().Select(x => x.ClrType).ToImmutableHashSet();
            foreach (var type in types)
            {
                if (entityTypes.Contains(type))
                {
                    continue;
                }
                modelBuilder.Model.AddEntityType(type);
            }
        }

        public virtual bool InThisDbContext(Type type)
        {
            return true;
        }
    }
}
